package com.canbot.u05.activity.guide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.presidemode.HostModeCustomModuleActivity;
import com.canbot.u05.activity.presidemode.PresideChooseAction;
import com.canbot.u05.bean.guide.MapReachVoice;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.StatusBarUtil;
import com.canbot.u05.utils.ToastTools;
import com.makeramen.roundedimageview.RoundedImageView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * 导览导点执行节点设置
 */
public class GuideReachSetActivity extends BaseActivityNoTitle {

        @BindView(R.id.main_iv_back)
        Button mainIvBack;

        @BindView(R.id.name_tv)
        TextView nameTv;

        @BindView(R.id.name_iv)
        ImageView nameIv;

        @BindView(R.id.save_bt)
        Button saveBt;

        @BindView(R.id.reach_voice_riv)
        RoundedImageView reachVoiceRiv;

        @BindView(R.id.reach_voice_tv)
        TextView reachVoiceTv;

        @BindView(R.id.reach_move_riv)
        RoundedImageView reachMoveRiv;

        @BindView(R.id.reach_move_tv)
        TextView reachMoveTv;

        @BindView(R.id.reach_pic_riv)
        RoundedImageView reachPicRiv;

        @BindView(R.id.reach_pic_tv)
        TextView reachPicTv;

        private MapReachVoice mapReachVoice;

        private Dialog renameDialog;

        private ToastTools toastTools;

        public static final int REQ_ADD_VOICE = 1001;

        public static final int REQ_ADD_ACTION = 1002;

        public static final int REQ_ADD_PIC = 1003;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.guide_reach_set);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                initView();
        }

        private void initView() {
                String reachInfo = getIntent().getStringExtra("reachInfo");
                if (TextUtils.isEmpty(reachInfo)) {
                        String name = getIntent().getStringExtra("name");
                        mapReachVoice = new MapReachVoice();
                        mapReachVoice.setReachName(name);
                }
                else {
                        mapReachVoice = JSON.parseObject(reachInfo, MapReachVoice.class);
                }
                nameTv.setText(mapReachVoice.getReachName());
                reachVoiceTv.setText(mapReachVoice.getReachVoiceContent());
                reachMoveTv.setText(mapReachVoice.getReachActionName());
                reachPicTv.setText(mapReachVoice.getImagePath());
                String imagePath = mapReachVoice.getImagePath();
                if (!TextUtils.isEmpty(imagePath)) {
                        imagePath = imagePath.replace("/uupic/", "/uupic_thumb/");
                }
                GlideApp.with(this).clear(reachPicRiv);
                GlideApp.with(this)
                        .asDrawable()
                        .load(IpMsgConst.HTTP_URL_CHEST + imagePath)
                        .thumbnail(0.1f)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                        .skipMemoryCache(false)
                        .placeholder(R.drawable.point_image_default)//占位图
                        .error(R.drawable.point_image_default)
                        .override(100, 100)
                        .into(reachPicRiv);
        }

        @OnClick({R.id.main_iv_back, R.id.name_iv, R.id.save_bt, R.id.reach_voice_riv, R.id.reach_move_riv, R.id.reach_pic_riv})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.main_iv_back:
                                finish();
                                break;
                        case R.id.name_iv:
                                showRenameDialog();
                                break;
                        case R.id.save_bt:
                                if (TextUtils.isEmpty(mapReachVoice.getReachVoiceContent())) {
                                        toastTools.showText("任务语音不可为空哦!");
                                        return;
                                }
                                Intent result = new Intent();
                                result.putExtra("reachInfo", JSON.toJSONString(mapReachVoice));
                                setResult(RESULT_OK, result);
                                finish();
                                break;
                        case R.id.reach_voice_riv:
                                Intent voiceIntent = new Intent(this, HostModeCustomModuleActivity.class);
                                voiceIntent.putExtra("path", mapReachVoice.getReachVoiceContent());
                                voiceIntent.putExtra("content", mapReachVoice.getReachVoiceContent());
                                voiceIntent.putExtra("type", mapReachVoice.getReachVoiceType());
                                voiceIntent.putExtra("isEnableNull", false);
                                startForResultActivity(voiceIntent, REQ_ADD_VOICE);
                                break;
                        case R.id.reach_move_riv:
                                Intent intent = new Intent(this, PresideChooseAction.class);
                                intent.putExtra("actionID", mapReachVoice.getReachActionId());
                                intent.putExtra("actionName", mapReachVoice.getReachActionName());
                                startForResultActivity(intent, REQ_ADD_ACTION);
                                break;
                        case R.id.reach_pic_riv:
                                Intent picIntent = new Intent(this, GuidePicSetActivity.class);
                                picIntent.putExtra("picPath", mapReachVoice.getImagePath());
                                startForResultActivity(picIntent, REQ_ADD_PIC);
                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == RESULT_OK) {
                        switch (requestCode) {
                                case REQ_ADD_VOICE:
                                        int type = data.getIntExtra("type", -1);
                                        String audioPath = data.getStringExtra("path");
                                        String content = data.getStringExtra("content");
                                        mapReachVoice.setReachVoiceType(type);
                                        mapReachVoice.setReachVoiceContent(audioPath);
                                        reachVoiceTv.setText(audioPath);
                                        break;
                                case REQ_ADD_ACTION:
                                        String actionID = data.getStringExtra("actionID");
                                        String actionName = data.getStringExtra("actionName");
                                        mapReachVoice.setReachActionName(actionName);
                                        mapReachVoice.setReachActionId(actionID);
                                        reachMoveTv.setText(actionName);
                                        break;
                                case REQ_ADD_PIC:
                                        String picPath = data.getStringExtra("picPath");
                                        mapReachVoice.setImagePath(picPath);
                                        reachPicTv.setText(picPath);
                                        String imagePath = mapReachVoice.getImagePath();
                                        if (!TextUtils.isEmpty(imagePath)) {
                                                imagePath = imagePath.replace("/uupic/", "/uupic_thumb/");
                                        }
                                        GlideApp.with(this)
                                              .asDrawable()
                                             .load(IpMsgConst.HTTP_URL_CHEST + imagePath)
                                             .thumbnail(0.1f)
                                             .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                                             .skipMemoryCache(false)
                                             .placeholder(R.drawable.point_image_default)//占位图
                                             .error(R.drawable.point_image_default)
                                             .override(100, 100)
                                             .into(reachPicRiv);
                                        break;
                        }
                }
        }

        /**
         * 重命名的 EditText
         */
        private EditText renameEditText;

        /**
         * 提示Dialog
         */
        private void showRenameDialog() {
                if (renameDialog == null) {
                        renameDialog = new Dialog(this, R.style.MyDialogStyle);
                        renameDialog.setContentView(R.layout.guide_dialog_rename);
                        renameEditText = (EditText) renameDialog.findViewById(R.id.guide_dialog_et);
                        renameDialog.findViewById(R.id.guide_dialog_rename_bt_sure).setOnClickListener(onClickListener);
                        renameDialog.findViewById(R.id.guide_dialog_rename_bt_cancel).setOnClickListener(onClickListener);
                        renameDialog.findViewById(R.id.guide_dialog_et_hint).setVisibility(View.GONE);
                }
                renameEditText.setText(nameTv.getText());
                StatusBarUtil.hideVirtualKey(renameDialog);
                renameDialog.show();
        }

        private View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        switch (v.getId()) {
                                case R.id.guide_dialog_rename_bt_sure:
                                        String trim = renameEditText.getText().toString().trim();
                                        if (TextUtils.isEmpty(trim)) {
                                                toastTools.showText(getString(R.string.please_enter_content));
                                                return;
                                        }
                                        mapReachVoice.setReachName(trim);
                                        nameTv.setText(trim);
                                        renameDialog.dismiss();
                                        break;
                                case R.id.guide_dialog_rename_bt_cancel:
                                        renameDialog.dismiss();
                                        break;
                        }
                }
        };

        @Override
        protected void onDestroy() {
                super.onDestroy();
                EventBus.getDefault().unregister(this);
        }

}